/**
 * @file 01-union-type.ts
 *
 * 1.3.1 借助联合类型使用不同的类型
 * 其中 UnionRangeValidation 能更优雅的实现多种不同类型的数据判断其区间
 */
export class RangeValidationBase {
    constructor(private start: number, private end: number) { }
    // private start:number = 0;
    // private end:number = 0;
    // constructor(start: number, end: number) {
    //     this.start = start;
    //     this.end = end
    // }

    protected RangeCheck(value: number): boolean {
        return value >= this.start && value <= this.end;
    }

    protected GetNumber(value: string): number {
        return new Number(value).valueOf();
    }
}

export class SparateTypeRangeValidation extends RangeValidationBase {
    IsInRangeString(value: string): boolean {
        return this.RangeCheck(this.GetNumber(value));
    }
    IsInRangeNumber(value: number): boolean {
        return this.RangeCheck(value);
    }
}

export class AnyRangeValidation extends RangeValidationBase {
    IsInRange(value: any): boolean {
        if (typeof value === "number") {
            return this.RangeCheck(value);
        } else if (typeof value === "string") {
            return this.RangeCheck(this.GetNumber(value));
        }
        return false;
    }
}

/**
 * 判断String类型数据 或 number 类型数据 是否在区间内
 *
 * Example:
 *       let unionRange = new UnionRangeValidation(10, 20)
 *       console.log(unionRange.IsInRange("30"))    flase
 *       console.log(unionRange.IsInRange(13)       true
 *       console.log(unionRange.IsInRange(9))       false
 *       console.log(unionRange.IsInRange(10))      true
 *       console.log(unionRange.IsInRange(20))      true
 *       console.log(unionRange.IsInRange(21))      false
 */
export class UnionRangeValidation extends RangeValidationBase {
    IsInRange(value: string | number): boolean {
        if (typeof value === "number") {
            return this.RangeCheck(value);
        }
        return this.RangeCheck(this.GetNumber(value));
    }
}

let sparateRange = new SparateTypeRangeValidation(10, 20);
console.log("SparateTypeRangeValidation");
console.log(sparateRange.IsInRangeString("30"));
console.log(sparateRange.IsInRangeNumber(13));
console.log(sparateRange.IsInRangeNumber(9));
console.log(sparateRange.IsInRangeNumber(10));
console.log(sparateRange.IsInRangeNumber(20));
console.log(sparateRange.IsInRangeNumber(21));

let anyRange = new AnyRangeValidation(11, 20);
console.log("AnyRangeValidation");
console.log(anyRange.IsInRange("30"));
console.log(anyRange.IsInRange(13));
console.log(anyRange.IsInRange(9));
console.log(anyRange.IsInRange(10));
console.log(anyRange.IsInRange(20));
console.log(anyRange.IsInRange(21));
console.log(anyRange.IsInRange(false));

let unionRange = new UnionRangeValidation(11, 20);
console.log("UnionRangeValidation");
console.log(unionRange.IsInRange("30"));
console.log(unionRange.IsInRange(13));
console.log(unionRange.IsInRange(9));
console.log(unionRange.IsInRange(10));
console.log(unionRange.IsInRange(20));
console.log(unionRange.IsInRange(21));
// console.log(unionRange.IsInRange(false))
